/*----------------------------------------\ | In word document, to merge cells within a table horizontally or | | vertically; | |-------------------------------------------| |--------------------------------------------------------------------| |---------------------------| | position=rN1cN2:rN3cN4; | | the merge start position is the N1th row and N2th column; | | the merge end position is the N3th row and N4th column; | | nrows: number of rows to be merged; if a dataset is provided, it | | will automatically merge the number of rows based on the | | number of observations in the dataset; | | ncols: number of columns to be merged; if a dataset is provided, | | it will automatically merge the number of columns based on | | the number of observations in the dataset; | | wordref: word reference; not necessary default is "wordsys"; | | Note: if row number and column number after ":" (rN3cN4) is not | | provided, user can alternatively specify number of rows to | | be merged ("nrows=") and number of columns to be merged | | ("ncols=") to accomplish similar result; | | So it is not necessary to specify in both ways; | | Note: the merge end row and end columns must be larger then start | | row and start column (the end position must be on the right | | down direction of the start position), which means the nrows| | and ncols must be greater than or equal to 0; | |-----------------------------| |--------------------------------------------------------------------| |---------------------------------------| | Example: %mergecells(position=r2c3:r3c43 r5c6:r6:c8); / | | %mergecells(position=r2c3, nrows=3, ncols=2); | | Usage: %mergecells(position=,nrows=1,ncols=1,wordref=wordsys); | \----------------------------------------*/ %macro mergecells(position=,nrows=,ncols=,wordref=wordsys); /*--------------------------------------------\ | Copy Right: Duo Zhou; | | Created: 2-27-2001 7:33pm; | | Modified: 7-15-2001 10:10pm; | | Purpose: Merge cells of the table in the | | word document; | \--------------------------------------------*/ %local nrows ncols numrows numcols _ir_ _ic_ _i_ _j_ _k_ npos _ij_ dummycolpos dummyrowpos; %let npos=%words(&position); %do _ij_=1 %to &npos; %let position&_ij_=%qscan(&position,&_ij_,%str( )); %let startpos=%qscan(&&position&_ij_,1,%str(():, )); %let startrowpos=%qscan(&startpos,1,%str(r c R C)); %let startcolpos=%qscan(&startpos,2,%str(r c R C)); %let endpos=%qscan(&&position&_ij_,2,%str(():, )); %let endrowpos=%qscan(&endpos,1,%str(r c R C)); %let endcolpos=%qscan(&endpos,2,%str(r c R C)); %if (&endrowpos ne) and (&endcolpos ne) %then %do; %if (&endrowpos < &startrowpos) %then %do; %let dummyrowpos=&endrowpos; %let endrowpos=&startrowpos; %let startrowpos=&dummyrowpos; %end; %if (&endcolpos < &startcolpos) %then %do; %let dummycolpos=&endcolpos; %let endcolpos=&startcolpos; %let startcolpos=&dummycolpos; %end; %let nrows=%eval(&endrowpos-&startrowpos); %let ncols=%eval(&endcolpos-&startcolpos); %end; %else %do; %if (%quote(&nrows) ne) and (%quote(%chk_type(&nrows)) eq %quote(2)) %then %do; %if (%sysfunc(exist(&nrows))) %then %do; %let dsid=%sysfunc(open(&nrows)); %let nrows=%sysfunc(attrn(&dsid,NOBS)); %let rc=%sysfunc(close(&dsid)); %end; %end; %if (%quote(&ncols) ne) and (%quote(%chk_type(&ncols)) eq %quote(2)) %then %do; %if (%sysfunc(exist(&ncols))) %then %do; %let dsid=%sysfunc(open(&ncols)); %let ncols=%sysfunc(attrn(&dsid,NOBS)); %let rc=%sysfunc(close(&dsid)); %end; %end; %end; %let numrows=%eval(%sysfunc(abs(&startrowpos))-1); %let numcols=%eval(%sysfunc(abs(&startcolpos))-1); %if (%quote(&nrows) ne) or (%quote(&ncols) ne) %then %do; %if (%quote(&nrows) ne) %then %do; %if &nrows <0 %then %do; %put ==> Alert! Either number of rows(=&nrows) is less than 0: ; %put ==> row position(=&endrowpos) is less then start row; %put ==> position(=&startrowpos).; %end; %end; %if (%quote(&ncols) ne) %then %do; %if &ncols <0 %then %do; %put ==> Alert! Either number of columns(=&ncols) is less than 0: ; %put ==> column position(=&endcolpos) is less then start column; %put ==> position(=&startcolpos).; %end; %end; /*%put ncols=&ncols;*/ data _null_; file &wordref lrecl=2000; put '[TableSelectTable]'; %if (&startrowpos > 0) %then %do; put '[StartOfColumn]'; %end; %else %do; put '[EndOfColumn]'; %end; %do _ir_=1 %to &numrows; put '[TableSelectCell]'; %if (&startrowpos >0) %then %do; put '[EditGoTo .Destination = "l+1"]'; %end; %else %do; put '[EditGoTo .Destination = "l-1"]'; %end; %end; %if (&startcolpos > 0) %then %do; put '[StartOfRow]'; %end; %else %do; put '[EndOfRow]'; %end; %do _ic_=1 %to &numcols; put '[TableSelectCell]'; %if (&startcolpos >0) %then %do; put '[NextCell]'; %end; %else %do; put '[PrevCell]'; %end; %end; %if (%quote(&ncols) ne) %then %do; %if &ncols >0 %then %do; %do _i_=1 %to &ncols; put '[TableSelectCell]'; put '[CharRight 1, 1]'; %end; %end; %end; %if (%quote(&nrows) ne) %then %do; %if &nrows >0 %then %do; %do _j_=1 %to &nrows; put '[TableSelectCell]'; put '[LineDown 1, 1]'; %end; %end; %end; put '[TableMergeCells]'; put '[TableRowHeight .RulerStyle = "0", .LineSpacingRule = 0, .LineSpacing = "", .LeftIndent = "0" + Chr$(34), .Alignment = 0, .AllowRowSplit = 0]'; run; %end; %else %do; %put ==> Alert! Check your input: nrows or ncols has no values. ; %end; %end; %mend mergecells;